package com.apple.learn.nio.buffer;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.CharBuffer;

public class BufferFillDrain {

	private static  int index = 0;
	private static final String[] strings = {
		"A random string value", 
		"The product of an infinite number of monkeys", 
		"Hey hey we're the Monkees", 
		"Opening act for the Monkees: Jimi Hendrix", 
		"'Scuse me while I kiss this fly", // Sorry Jimi ;-)
		"Help Me! Help Me!",
	};

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		CharBuffer buffer = CharBuffer.allocate(100);
		while(fillBuffer(buffer)){
			System.out.println("-----------");
			buffer.flip();
			drainBuffer(buffer);
			buffer.clear();
		}
		
		ByteOrder bor = buffer.order();
		System.out.println("native order:"+bor.toString());
		
		ByteBuffer bbf = ByteBuffer.allocate(100);
		bor = bbf.order();
		System.out.println("order:"+bor.toString());
	}

	private static void drainBuffer(CharBuffer buffer) {
		while(buffer.hasRemaining()){
			System.out.println(buffer.get());
		}
		System.out.println("");
	}

	private static boolean fillBuffer(CharBuffer buffer) {
		if(index >= strings.length ){
			return false;
		}
		String string = strings[index++];
		for (int i = 0; i < string.length(); i++) {
			buffer.put(string.charAt(i));
		}
		return true;
	}

}
